package controllers;

import java.util.List;

import models.*;
import play.mvc.*;

@With(Secure.class)
public class Courses extends Controller {

    public static void index() {
    	User user = Security.getConnected();
    	List courses = Course.find("order by name asc").fetch();
    	courses.removeAll(user.attendedCourses);
        render(user, courses);
    }

    public static void show(Long courseId) {
        Course course = Course.findById(courseId);
        notFoundIfNull(course);
        render(course);
    }
    
    public static void attendCourse(Long courseId) {
    	User user = Security.getConnected();
    	Course course = Course.findById(courseId);
    	if (user != null)
    		user.attendCourse(course);
    	index();
    }
    
    public static void leaveCourse(Long courseId) {
    	User user = Security.getConnected();
    	Course course = Course.findById(courseId);
    	if (user != null)
    		user.leaveCourse(course);
    	index();
    }

}
